package com.ruoyi.system.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.XaWuzi;
import com.ruoyi.system.service.IXaWuziService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.domain.Ztree;

/**
 * 物资Controller
 * 
 * @author ruoyi
 * @date 2020-11-08
 */
@Controller
@RequestMapping("/system/wuzi")
public class XaWuziController extends BaseController
{
    private String prefix = "system/wuzi";

    @Autowired
    private IXaWuziService xaWuziService;

    @RequiresPermissions("system:wuzi:view")
    @GetMapping()
    public String wuzi()
    {
        return prefix + "/wuzi";
    }

    /**
     * 查询物资树列表
     */
    @RequiresPermissions("system:wuzi:list")
    @PostMapping("/list")
    @ResponseBody
    public List<XaWuzi> list(XaWuzi xaWuzi)
    {
        List<XaWuzi> list = xaWuziService.selectXaWuziList(xaWuzi);
        return list;
    }

    /**
     * 导出物资列表
     */
    @RequiresPermissions("system:wuzi:export")
    @Log(title = "物资", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(XaWuzi xaWuzi)
    {
        List<XaWuzi> list = xaWuziService.selectXaWuziList(xaWuzi);
        ExcelUtil<XaWuzi> util = new ExcelUtil<XaWuzi>(XaWuzi.class);
        return util.exportExcel(list, "wuzi");
    }

    /**
     * 新增物资
     */
    @GetMapping(value = { "/add/{id}", "/add/" })
    public String add(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("xaWuzi", xaWuziService.selectXaWuziById(id));
        }
        return prefix + "/add";
    }

    /**
     * 新增保存物资
     */
    @RequiresPermissions("system:wuzi:add")
    @Log(title = "物资", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(XaWuzi xaWuzi)
    {
        return toAjax(xaWuziService.insertXaWuzi(xaWuzi));
    }

    /**
     * 修改物资
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        XaWuzi xaWuzi = xaWuziService.selectXaWuziById(id);
        mmap.put("xaWuzi", xaWuzi);
        return prefix + "/edit";
    }

    /**
     * 修改保存物资
     */
    @RequiresPermissions("system:wuzi:edit")
    @Log(title = "物资", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(XaWuzi xaWuzi)
    {
        return toAjax(xaWuziService.updateXaWuzi(xaWuzi));
    }

    /**
     * 删除
     */
    @RequiresPermissions("system:wuzi:remove")
    @Log(title = "物资", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{id}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("id") Long id)
    {
        return toAjax(xaWuziService.deleteXaWuziById(id));
    }

    /**
     * 选择物资树
     */
    @GetMapping(value = { "/selectWuziTree/{id}", "/selectWuziTree/" })
    public String selectWuziTree(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("xaWuzi", xaWuziService.selectXaWuziById(id));
        }
        return prefix + "/tree";
    }

    /**
     * 加载物资树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = xaWuziService.selectXaWuziTree();
        return ztrees;
    }
}
